package cn.snowheart

import groovy.text.SimpleTemplateEngine
import groovy.text.StreamingTemplateEngine
import groovy.text.XmlTemplateEngine

/**
 * 模板渲染引擎
 *
 * @author liuwanxiang
 * @version 2019/05/31
 */
class TemplateEngineExample {

    static void main(String[] args) {

        // 字符串中的模板
        def str = "World"
        println "Hello ${str}"

        // 简单模板引擎，注意需要使用单引号包围，同时属性不加大括号
        def template = 'Dear $name, your ordered $order had send out~'
        def binding = ["name": "Jackson", "order": "sofa"]
        def engine = new SimpleTemplateEngine()
        println engine.createTemplate(template).make(binding)

        // 流式模板引擎，可处理大于64k的字符串
        template = 'Dear <% out.print name %>, your ordered ${order} had send out~'
        engine = new StreamingTemplateEngine()
        println engine.createTemplate(template).make(binding)


        binding = [StudentName: 'Joe', id: 1, subject: 'Physics']
        engine = new XmlTemplateEngine()
        def text = '''
   <document xmlns:gsp='http://groovy.codehaus.org/2005/gsp'>
      <Student>
         <name>${StudentName}</name>
         <ID>${id}</ID>
         <subject>${subject}</subject>
      </Student>
   </document> 
'''
        template = engine.createTemplate(text).make(binding)
        println template.toString()

    }


}
